package com.walkline.util.ui;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.AutoTextEditField;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class MyTextField extends AutoTextEditField
{
	public static final int MODE_START = 0;
	public static final int MODE_FINISH = 1;
	public static final int CLICKED = 100;

	private int _mode = 0;

	public MyTextField(int mode)
	{
		super("", null, DEFAULT_MAXCHARS, USE_ALL_HEIGHT | USE_ALL_WIDTH | JUMP_FOCUS_AT_END | NO_NEWLINE);

		_mode = mode;

		XYEdges edge = new XYEdges(4, 4, 4, 4);
		Border borderNormal = BorderFactory.createRoundedBorder(edge, 0xBBBBBB, Border.STYLE_SOLID);
		Border borderFocus = BorderFactory.createRoundedBorder(edge, 0x0A9000, Border.STYLE_SOLID);
		Background background = BackgroundFactory.createSolidBackground(Color.WHITE);

		this.setBackground(background);
		this.setBorder(VISUAL_STATE_NORMAL, borderNormal);
		this.setBorder(VISUAL_STATE_FOCUS, borderFocus);
	}

	protected void paint(Graphics g)
	{
		super.paint(g);

		if (getTextLength() == 0)
		{
			g.setColor(Color.GRAY);
			g.drawText(_mode == MODE_START ? " 输入起点（按回车开始查询）" : " 输入终点（按回车开始查询）", 0, 0);			
		}
	}

//	protected void layout(int width, int height)
//	{
//		super.layout(width, height);
//		super.setExtent(width, Font.getDefault().getHeight() * 7);
//	}

//	protected boolean keyChar(char key, int status, int time)
//	{
//		invalidate();
//		return super.keyChar(key, status, time);
//	}

	protected boolean keyChar(char character, int status, int time) 
    {
    	switch (character)
    	{
			case Characters.ENTER:
	            clickButton();
	            return true;
        }

        return super.keyChar(character, status, time);
    }

    protected boolean navigationUnclick(int status, int time) 
    {
    	if ((status & KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY)
    	{
        	clickButton();
        	return true;
    	}

    	return super.navigationClick(status, time);
    }

    protected boolean trackwheelClick(int status, int time)
    {
    	if ((status & KeypadListener.STATUS_TRACKWHEEL) == KeypadListener.STATUS_TRACKWHEEL)
    	{
       		clickButton();
       		return true;
    	}

    	return super.trackwheelClick(status, time);
    }

    protected boolean invokeAction(int action) 
    {
    	switch(action)
    	{
    		case ACTION_INVOKE:
           		clickButton();
           		return true;
    	}

    	return super.invokeAction(action);
    }

	public void clickButton() {fieldChangeNotify(CLICKED);}

	protected void displayFieldFullMessage() {}

    public void setDirty(boolean dirty) {}

    public void setMuddy(boolean muddy) {}
}